package com.yy.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

/**
 * 应用程序配置类：用于保存用户相关信息及设置
 */
@SuppressLint("NewApi")
public class AppConfig {

	private final static String APP_CONFIG = "config";
	public final static String DB_NAME = "gangfutong.db";//数据库名称

	public final static String BASE_UPTIME="2015-01-01 00:00:00";//系统最后一次更新基础数据默认时间

	public final static String CONF_APP_UNIQUEID = "APP_UNIQUEID";// APP唯一码
	public final static String CONF_COOKIE = "cookie";// cookie
	public final static String CONF_LOAD_IMAGE = "perf_loadimage";// 是否加载图片
	public final static String CONF_SCROLL = "perf_scroll";// 是否左右滑动
	public final static String CONF_HTTPS_LOGIN = "perf_httpslogin";// 是否https登陆
	public final static String CONF_VOICE = "perf_voice";// 是否开启提示音
	public final static String CONF_CHECKUP = "perf_checkup";// 是否启动检查更新
	public final static String CONF_AppToken = "appToken";// 应用程序标识码
	public final static String CONF_AppSecret = "appSecret";// 应用程序秘钥
	 
    private String packageName = ""; 
    private SharedPreferences sharedPreference;  


	public final static String SAVE_IMAGE_PATH = "save_image_path";// 保存图标地址
	@SuppressLint("NewApi")
	public final static String DEFAULT_SAVE_IMAGE_PATH = Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "LoanService"
			+ File.separator;

	private Context mContext;// 应用程序上下文
	private static AppConfig appConfig;// app配置

	public static AppConfig getAppConfig(Context context) {
		if (appConfig == null) {
			appConfig = new AppConfig(context);
			appConfig.mContext = context;
		}
		return appConfig;
	}
	
	public AppConfig(Context context){  
        packageName = context.getPackageName() + "_preferences";  
        sharedPreference = context.getSharedPreferences(  
                packageName, context.MODE_PRIVATE);  
    }  


	/**
	 * 获取Preference设置
	 */
	public static SharedPreferences getSharedPreferences(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context);
	}

	/**
	 * 是否加载显示文章图片
	 */
	public static boolean isLoadImage(Context context) {
		return getSharedPreferences(context).getBoolean(CONF_LOAD_IMAGE, true);
	}

	/* 获取Cookie */
	public String getCookie() {
		return get(CONF_COOKIE);
	}

	public void setAppToken(String appToken) {
		set(CONF_AppToken, appToken);
	}

	public String getAppToken() {
		return get(CONF_AppToken);
	}

	public void setAppSecret(String appSecret) {
		set(CONF_AppSecret, appSecret);
	}

	public String getAppSecret() {
		return get(CONF_AppSecret);
	}

	/*配置文件操作*/
	public String get(String key) {
		Properties props = get();
		return (props != null) ? props.getProperty(key) : null;
	}

	public Properties get() {
		FileInputStream fis = null;
		Properties props = new Properties();
		try {
			// 读取files目录下的config
			// fis = activity.openFileInput(APP_CONFIG);

			// 读取app_config目录下的config
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			fis = new FileInputStream(dirConf.getPath() + File.separator
					+ APP_CONFIG);

			props.load(fis);
		} catch (Exception e) {
		} finally {
			try {
				fis.close();
			} catch (Exception e) {
			}
		}
		return props;
	}

	private void setProps(Properties p) {
		FileOutputStream fos = null;
		try {
			// 把config建在files目录下
			// fos = activity.openFileOutput(APP_CONFIG, Context.MODE_PRIVATE);

			// 把config建在(自定义)app_config的目录下
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			File conf = new File(dirConf, APP_CONFIG);
			fos = new FileOutputStream(conf);

			p.store(fos, null);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (Exception e) {
			}
		}
	}
	
	
	public void set(Properties ps) {
		Properties props = get();
		props.putAll(ps);
		setProps(props);
	}

	public void set(String key, String value) {
		Properties props = get();
		props.setProperty(key, value);
		setProps(props);
	}

	public void remove(String... key) {
		Properties props = get();
		for (String k : key)
			props.remove(k);
		setProps(props);
	}
}
